const fs = window.require('fs')
const fileHelper = {
  readFile: (path) => {
    return new Promise((resolve, reject) => {
      fs.readFile(path, { encoding: 'utf8' }, (err, data) => {
        if (!err) {
          resolve(data)
        } else {
          reject(err)
        }
      })
    })
  },
  writeFile: (path, data) => {
    return new Promise((resolve, reject) => {
      fs.writeFile(path, data, { encoding: 'utf8' }, (err) => {
        if (!err) {
          resolve(true)
        } else {
          reject(err)
        }
      })
    })
  },
  renameFile: (oldpath, newpath) => {
    return new Promise((resolve, reject) => {
      fs.rename(oldpath, newpath, (err) => {
        if (!err) {
          resolve(true)
        } else {
          reject(err)
        }
      })
    })
  },
  deleteFile: (path) => {
    return new Promise((resolve, reject) => {
      fs.unlink(path, (err) => {
        if (!err) {
          resolve(true)
        } else {
          reject(err)
        }
      })
    })
  }
}

export default fileHelper
